/*
 * NativeCallsDeclaration.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.delegate;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeGenerator;
import com.cute4j.tools.ng.Utils;

/**
 * Native Calls Declaration.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
class NativeCallsDeclaration extends NativeDeclarationBlock {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new NativeCallsDeclaration object.
   *
   * @param nativeFile the abstract native file
   * @param text the text
   * @param javaClass the java class
   */
  public NativeCallsDeclaration(AbstractNativeFile nativeFile, String text,
                                JavaClass javaClass) {
    super(nativeFile, text, javaClass);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public void store(PrintWriter pw) throws IOException {
    String delegateObjectName = Utils.getDelegateObjectName(getJavaClass());
    ArrayList textMethods = getMethodDeclarationsFromText();
    JavaMethod[] methods = getJavaClass().getMethods();
    boolean[] implementedMethods = new boolean[methods.length];
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      if (method.isNative()) {
        String delegateObject = Utils.getNativeTagParameter(method,
                                                            "delegateObject");
        implementedMethods[i] =
            ((Utils.getNativeTagParameter(method, "code") != null) ||
             ((delegateObject != null) &&
              !delegateObjectName.equals(delegateObject)));
      }
    }
    if (getText().indexOf(" static ") >= 0) {
      pw.println("public: // native calls");
      for (int i = 0; i < methods.length; i++) {
        JavaMethod method = methods[i];
        if (!implementedMethods[i] && method.isNative() && method.isStatic()) {
          String methodDeclaration =
                          Utils.getNativeCallStaticMethodDeclaration(method);
          int mdi = textMethods.indexOf(methodDeclaration);
          if (mdi >= 0) {
            textMethods.remove(mdi);
          }
          pw.println(methodDeclaration);
        }
      }
      for (int i = 0; i < methods.length; i++) {
        JavaMethod method = methods[i];
        if (!implementedMethods[i] && method.isNative() && !method.isStatic() &&
            "_create".equals(method.getName())) {
          String methodDeclaration =
                     Utils.getNativeCallNewInstanceMethodDeclaration(method);
          int mdi = textMethods.indexOf(methodDeclaration);
          if (mdi >= 0) {
            textMethods.remove(mdi);
          }
          pw.println(methodDeclaration);
        }
      }
    } else {
      String delegateConstructorName = "  " + 
                                       Utils.getDelegateName(getJavaClass()) +
                                       "(";
      if (getText().indexOf(delegateConstructorName) >= 0) {
        pw.println("protected:");
        for (int i = 0; i < methods.length; i++) {
          JavaMethod method = methods[i];
          if (!implementedMethods[i] && method.isNative() &&
              !method.isStatic() &&
              "_create".equals(method.getName())) {
            String methodDeclaration =
                               Utils.getDelegateConstructorDeclaration(method);
            int mdi = textMethods.indexOf(methodDeclaration);
            if (mdi >= 0) {
              textMethods.remove(mdi);
            }
            pw.println(methodDeclaration);
          }
        }
      } else {
        if (getText().startsWith("public: // native calls")) {
          pw.println("public: // native calls");
          for (int i = 0; i < methods.length; i++) {
            JavaMethod method = methods[i];
            if (!implementedMethods[i] && method.isNative() &&
                !method.isStatic() &&
                !"_create".equals(method.getName())) {
              String methodDeclaration =
                                  Utils.getNativeCallMethodDeclaration(method);
              int mdi = textMethods.indexOf(methodDeclaration);
              if (mdi >= 0) {
                textMethods.remove(mdi);
              }
              pw.println(methodDeclaration);
            }
          }
        } else {
          pw.println("public slots: // native calls");
          for (int i = 0; i < methods.length; i++) {
            JavaMethod method = methods[i];
            if (!implementedMethods[i] && method.isNative() &&
                !method.isStatic() &&
                !"_create".equals(method.getName())) {
              String methodDeclaration =
                                  Utils.getNativeCallMethodDeclaration(method);
              int mdi = textMethods.indexOf(methodDeclaration);
              if (mdi >= 0) {
                textMethods.remove(mdi);
              }
              pw.println(methodDeclaration);
            }
          }
        }
      }
    }
    String newLine = NativeGenerator.getInstance().getNewLine();
    String newLineComment =  newLine + "//";
    for (int i = 0; i < textMethods.size(); i++) {
      String methodText = (String)textMethods.get(i);
      if (methodText.startsWith("  ")) {
        methodText = methodText.replaceAll(newLine, newLineComment);
        pw.print("//");
      }
      pw.println(methodText);
    }
  }

}
